package com.Xht.www.servlet;

//后端响应管理员获取所有商家信息的请求，不筛选

import com.Xht.www.dao.MerchantInfoDAO;
import com.Xht.www.po.MerchantInfoPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/MerchantInfoServlet")
public class MerchantInfoServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        try (PrintWriter out = response.getWriter()) {
            // 使用新方法获取所有商家信息，不进行任何筛选
            MerchantInfoDAO merchantDAO = new MerchantInfoDAO();
            List<MerchantInfoPO> merchantList = merchantDAO.getAllMerchantsWithoutFilter();

            // 转换为JSON格式
            StringBuilder jsonResponse = new StringBuilder();
            jsonResponse.append("[");

            for (int i = 0; i < merchantList.size(); i++) {
                MerchantInfoPO merchant = merchantList.get(i);
                jsonResponse.append("{");
                jsonResponse.append("\"merchantId\":").append(merchant.getMerchantId()).append(",");
                jsonResponse.append("\"merchantName\":\"").append(escapeJson(merchant.getMerchantName())).append("\",");
                jsonResponse.append("\"address\":\"").append(escapeJson(merchant.getAddress())).append("\",");
                jsonResponse.append("\"establishmentYear\":").append(merchant.getEstablishmentYear()).append(",");
                jsonResponse.append("\"contactInfo\":\"").append(escapeJson(merchant.getContactInfo())).append("\",");
                jsonResponse.append("\"keywords\":\"").append(escapeJson(merchant.getKeywords())).append("\",");
                jsonResponse.append("\"businessStatus\":\"").append(escapeJson(merchant.getBusinessStatus())).append("\",");
                jsonResponse.append("\"applicationStatus\":\"").append(escapeJson(merchant.getApplicationStatus())).append("\",");
                jsonResponse.append("\"salesVolume\":").append(merchant.getSalesVolume() != null ? merchant.getSalesVolume() : "null").append(",");
                jsonResponse.append("\"favoritesCount\":").append(merchant.getFavoritesCount() != null ? merchant.getFavoritesCount() : "null").append(",");
                jsonResponse.append("\"turnover\":").append(merchant.getTurnover() != null ? merchant.getTurnover() : "null").append(",");
                jsonResponse.append("\"password\":\"").append(escapeJson(merchant.getPassword())).append("\"");
                jsonResponse.append("}");

                if (i < merchantList.size() - 1) {
                    jsonResponse.append(",");
                }
            }

            jsonResponse.append("]");

            // 返回JSON响应
            out.print(jsonResponse.toString());
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "获取商家信息失败");
        }
    }

    // 处理JSON转义字符
    private String escapeJson(String value) {
        if (value == null) return "";
        return value.replace("\\", "\\\\")
                .replace("\"", "\\\"")
                .replace("\b", "\\b")
                .replace("\f", "\\f")
                .replace("\n", "\\n")
                .replace("\r", "\\r")
                .replace("\t", "\\t");
    }
}
